home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 5562 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.2 KB

  1. Path: newshost.lanl.gov!tanmoy
  2. From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: accessing structures (newbie question)
  5. Date: 19 Feb 1996 16:08:45 GMT
  6. Organization: Los Alamos National Laboratory
  7. Message-ID: <TANMOY.96Feb19090845@qcd.lanl.gov>
  8. References: <4g8gic$o6u@news1.sunbelt.net> <3128FA60.5227@metagen.co.uk>
  9. NNTP-Posting-Host: qcd.lanl.gov
  10. Mime-Version: 1.0
  11. Content-Type: text
  12. In-reply-to: Rob Stenton's message of Mon, 19 Feb 1996 14:32:00 -0800
  13.  
  14. In article <3128FA60.5227@metagen.co.uk> Rob Stenton
  15. <rws@metagen.co.uk> writes: 
  16. <snip>
  17.    There are basically two ways of accessing elements within a malloced 
  18.    buffer:
  19.    1. Treat the malloced buffer as an array (as you suggested).
  20.    2. Increment a local pointer by sizeof(picture) in loop iteration.
  21.  
  22.    The first method is preferable because the second method involves pointer 
  23.    arithmetic (always best avoided) which can cause problems on different 
  24.    memory architectures and assumes things about BYTE sizes (but usually 
  25.    works).
  26.  
  27. What nonsense! Anything creates trouble if you do not take the time
  28. to understand it.
  29.  
  30.    I think you may have been using the wrong syntax when you tried the array 
  31.    method. The xth item is given by 'photos[x].' which is equivalent to 
  32.    '(photos + x * sizeof(picture))->'.
  33.  
  34. This is the problem!!! photos[x]. is always the same as (photos + x)->
  35. In fact, this is such a basic fact of pointer arithmetic that anyone
  36. who posts such nonsense must surely be trying to deliberately mislead
  37. newbies. We have enough confusion here: please refrain from posting
  38. _answers_ unless you know at least the basics of C! 
  39.  
  40. When an integer x is added to a pointer photos to a struct, the
  41. pointer is updated so as to skip over x structs, not x bytes
  42. as this poster maliciously suggested. 
  43.  
  44. Cheers
  45. Tanmoy
  46. --
  47. tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
  48. Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
  49. Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
  50. <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
  51. internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
  52. fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]
  53.